<template>
  <h3>Son 组件</h3>
  <hr />
  <p id="info">num 的值是：{{ num }}</p>
  <button @click="num++">+1</button>
  <!-- 为 ECharts 准备一个定义了宽高的 DOM -->
  <div id="main" style="width: 600px; height: 400px"></div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  data() {
    return {
      msg: 'hello',
      num: 0,
      timerId: null
    }
  },
  methods: {
    showNum() {
      console.log('num 的值是：' + this.num)
    },
    initEcharts() {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(document.getElementById('main'))
      // 绘制图表
      myChart.setOption({
        title: {
          text: 'ECharts 入门示例'
        },
        tooltip: {},
        xAxis: {
          data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']
        },
        yAxis: {},
        series: [
          {
            name: '销量',
            type: 'bar',
            data: [5, 20, 36, 10, 10, 20]
          }
        ]
      })
    }
  },
  computed: {
    numPlus2() {
      return this.num * 2
    }
  },
  setup() {
    console.log('执行了 setup 函数')
  },
  beforeCreate() {
    console.log('beforeCreate')
  },
  created() {
    console.log('created')
    // console.log(this.num)
    // this.showNum()
    // const dom = document.querySelector('#info')
    // console.log(dom)
  },
  beforeMount() {
    console.log('beforeMount')
  },
  mounted() {
    console.log('mounted')
    // 初始化Echarts图表
    this.initEcharts()

    this.timerId = setTimeout(() => {
      console.log('执行了 setTimeout 延时器的回调')
    }, 2000)
  },
  beforeUpdate() {
    console.log('beforeUpdate')
  },
  updated() {
    console.log('updated')
  },
  beforeUnmount() {
    console.log('beforeUnmount')
    clearTimeout(this.timerId)
  },
  unmounted() {
    console.log('unmounted')
  }
}
</script>
